import 'package:flutter/material.dart';
import 'package:flutter_boss_mine/eneity/position_entity.dart';
import 'package:flutter_boss_mine/http/api_response.dart';
import 'package:flutter_boss_mine/repository/position_repository.dart';
import 'package:flutter_boss_mine/utils/color_util.dart';
import 'package:flutter_boss_mine/widget/tag_button.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:random_color/random_color.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
const arrayList = [
  {
    "title": "IOS前端",
    "workingTM": "工作年限",
    "education": "本科",
    "Tags": [
      "Swift","Objective-C","IOS",
    ],
    "company": "海祈互动",
    "companyState": "不需要融资",
    "employ": "0-20人",
    "name": "许闯",
    "postion": "总经理",
    "address": "铁西区 铁西广场",
    "salary": "8-12K"
  }
];

class PositionTabbarPage extends StatefulWidget {
  @override
  _PositionTabbarPageState createState() => _PositionTabbarPageState();
}

class _PositionTabbarPageState extends State<PositionTabbarPage> with AutomaticKeepAliveClientMixin {
  List<PositionData> dataList = [];
  EasyRefreshController _controller = EasyRefreshController();
  int pageIndex = 1;
  int pageSize = 10;

  @override
  void initState() {
    _loadData(1, pageSize);
    super.initState();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: EasyRefresh(
        controller: _controller,
        onRefresh: () async {
          setState(() {
            pageIndex = 1;
          });
          _loadData(pageIndex, pageSize);
        },
        onLoad: () async {
          setState(() {
            pageIndex += 1;
          });
          _loadData(pageIndex, pageSize);
        },
        child: ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: dataList.length,
          itemBuilder: (context, index) {
            return _item(dataList[index]);
          },
        ),
      ),
    );
  }

  _loadData(int page, int pageSize) async {
    ApiResponse<PositionEntity> entity = await PositionRepository.getPositions(page, pageSize);
    List<PositionData> items = entity.data.data;
    setState(() {
      if (page == 1) {
        dataList.clear();
        _controller.finishRefresh(success: true);
        _controller.finishLoad(success: true, noMore: false);
      } else {
        if (page ==3) {
          _controller.finishLoad(success: true, noMore: true);
        } else {
          _controller.finishLoad(success: true, noMore: false);
        }
      }
      dataList.addAll(items);
    });

  }


  _item(PositionData item) {
    return Column(
      children: [
        Container(
          color: Colors.white,
          padding: EdgeInsets.only(left: 20, right: 20),
          height: 300.h,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(item.title, style: TextStyle(fontWeight: FontWeight.w500, fontSize: 22),),
                  Text(item.salary, style: TextStyle(fontSize: 18, color: ColorUtil.color("#23ACA8")),)
                ],
              ),
              Row(
                children: item.tags.map((e) {
                  return TagButton(e);
                }).toList()
              ),
              Row(
                children: [
                  Text(item.company),
                  SizedBox(width: 10,),
                  Text(item.companyState),
                  SizedBox(width: 10,),
                  Text(item.employ),
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      PhysicalModel(
                        color: Colors.transparent,
                        clipBehavior: Clip.antiAlias,
                        borderRadius: BorderRadius.circular(35),
                        child: Image.asset("resource/images/avatar_16.jpg", width: 30,),
                      ),
                      SizedBox(width: 8,),
                      Text(item.name),
                      Text("·"),
                      Text(item.postion)
                    ],
                  ),
                  Text(item.address),
                ],
              ),
              Opacity(opacity: 0.1,child: Container(color: Colors.black26, width: double.infinity,))

            ],
          ),
        ),
        Container(width: double.infinity, height: 5, color: Colors.black12,)

      ],
    );
  }
}
